package com.zysoft.system.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;

@Entity
@DiscriminatorValue("Tree")
public class ArgumentsTree extends ArgumentsBase {
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "parentId")
	private ArgumentsTree parent;
	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "parent")
	@OrderBy("orderBy")
	private Set<ArgumentsTree> childSet;

	public ArgumentsTree getParent() {
		return parent;
	}

	public void setParent(ArgumentsTree parent) {
		this.parent = parent;
	}

	public synchronized Set<ArgumentsTree> getChildSet() {
		if (childSet == null)
			childSet = new HashSet<ArgumentsTree>();
		return childSet;
	}

	public void setChildSet(Set<ArgumentsTree> childSet) {
		this.childSet = childSet;
	}

	public void addChild(ArgumentsTree child) {
		if(child != null){
			child.setParent(this);
			getChildSet().add(child);
		}
	}

	public void removeChild(ArgumentsTree child) {
		if (child!=null && getChildSet().contains(child)) {
			child.setParent(null);
			getChildSet().remove(child);
		}
	}
}
